using System;
using System.Reflection;
using UnityEngine;
using UniRx;
using GG.Tool;

#if UNITY_EDITOR
using UnityEditor;

namespace GG.Tool
{
    /// <summary>
    /// 专门处理 IntReactiveProperty 的 PropertyDrawer
    /// </summary>
    [CustomPropertyDrawer(typeof(IntReactiveProperty))]
    public class IntReactivePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // 检查是否有 ChineseName 特性
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                // 查找 ReactiveProperty 内部的 value 字段
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                
                if (valueProperty != null)
                {
                    // 创建带中文名称的 GUI 内容
                    GUIContent newLabel = new GUIContent(chineseNameAttr.displayName);
                    
                    // 使用新标签绘制 value 属性
                    EditorGUI.PropertyField(position, valueProperty, newLabel);
                }
                else
                {
                    // 如果找不到 value 字段，使用默认绘制
                    EditorGUI.PropertyField(position, property, label);
                }
            }
            else
            {
                // 没有 ChineseName 特性，使用默认绘制
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                if (valueProperty != null)
                {
                    return EditorGUI.GetPropertyHeight(valueProperty, label);
                }
            }
            
            return EditorGUI.GetPropertyHeight(property, label);
        }
    }

    /// <summary>
    /// 专门处理 DoubleReactiveProperty 的 PropertyDrawer
    /// </summary>
    [CustomPropertyDrawer(typeof(DoubleReactiveProperty))]
    public class DoubleReactivePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                
                if (valueProperty != null)
                {
                    GUIContent newLabel = new GUIContent(chineseNameAttr.displayName);
                    EditorGUI.PropertyField(position, valueProperty, newLabel);
                }
                else
                {
                    EditorGUI.PropertyField(position, property, label);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                if (valueProperty != null)
                {
                    return EditorGUI.GetPropertyHeight(valueProperty, label);
                }
            }
            
            return EditorGUI.GetPropertyHeight(property, label);
        }
    }

    /// <summary>
    /// 专门处理 FloatReactiveProperty 的 PropertyDrawer
    /// </summary>
    [CustomPropertyDrawer(typeof(FloatReactiveProperty))]
    public class FloatReactivePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                
                if (valueProperty != null)
                {
                    GUIContent newLabel = new GUIContent(chineseNameAttr.displayName);
                    EditorGUI.PropertyField(position, valueProperty, newLabel);
                }
                else
                {
                    EditorGUI.PropertyField(position, property, label);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                if (valueProperty != null)
                {
                    return EditorGUI.GetPropertyHeight(valueProperty, label);
                }
            }
            
            return EditorGUI.GetPropertyHeight(property, label);
        }
    }

    /// <summary>
    /// 专门处理 BoolReactiveProperty 的 PropertyDrawer
    /// </summary>
    [CustomPropertyDrawer(typeof(BoolReactiveProperty))]
    public class BoolReactivePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                
                if (valueProperty != null)
                {
                    GUIContent newLabel = new GUIContent(chineseNameAttr.displayName);
                    EditorGUI.PropertyField(position, valueProperty, newLabel);
                }
                else
                {
                    EditorGUI.PropertyField(position, property, label);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                if (valueProperty != null)
                {
                    return EditorGUI.GetPropertyHeight(valueProperty, label);
                }
            }
            
            return EditorGUI.GetPropertyHeight(property, label);
        }
    }

    /// <summary>
    /// 专门处理 StringReactiveProperty 的 PropertyDrawer
    /// </summary>
    [CustomPropertyDrawer(typeof(StringReactiveProperty))]
    public class StringReactivePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                
                if (valueProperty != null)
                {
                    GUIContent newLabel = new GUIContent(chineseNameAttr.displayName);
                    EditorGUI.PropertyField(position, valueProperty, newLabel);
                }
                else
                {
                    EditorGUI.PropertyField(position, property, label);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                if (valueProperty != null)
                {
                    return EditorGUI.GetPropertyHeight(valueProperty, label);
                }
            }
            
            return EditorGUI.GetPropertyHeight(property, label);
        }
    }

    /// <summary>
    /// 专门处理 Vector3ReactiveProperty 的 PropertyDrawer
    /// </summary>
    [CustomPropertyDrawer(typeof(Vector3ReactiveProperty))]
    public class Vector3ReactivePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                
                if (valueProperty != null)
                {
                    GUIContent newLabel = new GUIContent(chineseNameAttr.displayName);
                    EditorGUI.PropertyField(position, valueProperty, newLabel);
                }
                else
                {
                    EditorGUI.PropertyField(position, property, label);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            ChineseNameAttribute chineseNameAttr = fieldInfo.GetCustomAttribute<ChineseNameAttribute>();
            
            if (chineseNameAttr != null)
            {
                SerializedProperty valueProperty = property.FindPropertyRelative("value");
                if (valueProperty != null)
                {
                    return EditorGUI.GetPropertyHeight(valueProperty, label);
                }
            }
            
            return EditorGUI.GetPropertyHeight(property, label);
        }
    }
}
#endif

